package com.eschool.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.converter.SchoolConverter;
import com.eschool.model.Branch;
import com.eschool.model.School;
import com.eschool.model.User;
import com.eschool.service.CustomerService;
import com.eschool.service.impl.CustomerServiceImpl;

@ManagedBean(name="branchBean")
@ViewScoped
public class BranchBean {
	

	private School selectedSchool;
	
	private List<School> allSchools;
	
	private SchoolConverter schoolConverter;

	CustomerService service = new CustomerServiceImpl();
	
	public BranchBean() {
		// TODO Auto-generated constructor stub
		selectedSchool = new School();
		schoolConverter = new SchoolConverter();
		allSchools = SchoolConverter.schoolDB;
	}

	public School getSelectedSchool() {
		return selectedSchool;
	}

	public void setSelectedSchool(School school) {
		this.selectedSchool = school;
		
	}

	public SchoolConverter getSchoolConverter() {
		return schoolConverter;
	}

	public void setSchoolConverter(SchoolConverter schoolConverter) {
		this.schoolConverter = schoolConverter;
	}

	public List<School> completeSchool(String query) {
		List<School> suggestions = new ArrayList<School>();  
        
        for(School s : allSchools) {
            if(s.getSchoolName().toLowerCase().startsWith(query.toLowerCase()))  
                suggestions.add(s);  
        }
          
        return suggestions; 
	}
	
	Branch branch = new Branch();
	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	User user = new User();
	CustomerService customerService = new CustomerServiceImpl();
	
	public String action()
	{
		branch.setSchool(selectedSchool);
		String message = "";
		if(service.addBranch(branch, user)) {
			message = "Branch Successfully Added";
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Success Message", message));
		}
		else {
			message = "Branch Not Added";
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Message", message));
		}
		
		return "AddBranch";
	}

}
